package ui.views;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.UIManager;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Font;
import java.awt.Color;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPasswordField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UIMain extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 345537946347006311L;

	private JPanel contentPane;
	private JLabel usernameLabel;
	private JPasswordField passwordField;
	private JButton loginButton;
	private JLabel passwordLabel;
	private JTextField usernameField;
	private JPanel welcomePanel;
	private JPanel bodyPanel;
	private JPanel loginPanel;
	private JButton cancelButton;
	private JPanel poweredPanel;
	private JLabel poweredLabel;
	private JPanel creditPanel;
	private JLabel creditLabel1;
	private JLabel creditLabel2;
	private Dimension dimension;
	
	//private static JOptionPane wrongCredentialPane;
	//private static JDialog wrongCredentialDialog;
	private static JOptionPane errorPane;
	private static JDialog errorDialog;
	
	/**
	 * class's singleton
	 */
	private static UIMain uiMainInstance;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIMain.getUIMainInstance().setVisible(true);
					UIMain.getUIMainInstance().addWindowListener(new java.awt.event.WindowAdapter(){
					@Override
					public void windowClosing(java.awt.event.WindowEvent windowEvent) {
						errorPane = new JOptionPane("        LEAVE ME ALONE", JOptionPane.ERROR_MESSAGE);
						errorDialog = errorPane.createDialog("STOP PLAYING ARROUND");
						new Thread(new Runnable(){
							public void run(){
								try{
									Thread.sleep(1500);
									errorDialog.dispose();
								}
								catch (Throwable e){
								}
							}
						}).start();
						errorDialog.setVisible(true);
					}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	private UIMain() {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(UIMain.class.getResource("/ui/recources/fridge.png")));
		setTitle("ESW Fridge");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 644, 528);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 204, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		dimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(dimension.width/2-getSize().width/2, dimension.height/2-getSize().height/2);
		
		welcomePanel = new JPanel();
		welcomePanel.setBackground(new Color(0, 204, 204));
		
		bodyPanel = new JPanel();
		bodyPanel.setBackground(new Color(0, 204, 204));
		
		poweredPanel = new JPanel();
		poweredPanel.setBackground(new Color(0, 204, 204));
		
		poweredLabel = new JLabel("powered by java");
		poweredLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
		GroupLayout gl_poweredPanel = new GroupLayout(poweredPanel);
		gl_poweredPanel.setHorizontalGroup(
			gl_poweredPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(poweredLabel, GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
		);
		gl_poweredPanel.setVerticalGroup(
			gl_poweredPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(poweredLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
		);
		poweredPanel.setLayout(gl_poweredPanel);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setIcon(new ImageIcon(UIMain.class.getResource("/ui/recources/member1.png")));
		
		passwordLabel = new JLabel("");
		passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
		passwordLabel.setIcon(new ImageIcon(UIMain.class.getResource("/ui/recources/password1.png")));
		
		usernameLabel = new JLabel("");
		usernameLabel.setHorizontalAlignment(SwingConstants.CENTER);
		usernameLabel.setIcon(new ImageIcon(UIMain.class.getResource("/ui/recources/username2.png")));
		
		passwordField = new JPasswordField();
		passwordField.setFont(new Font("Calibri", Font.PLAIN, 14));
		passwordField.setHorizontalAlignment(SwingConstants.CENTER);
		
		usernameField = new JTextField();
		usernameField.setFont(new Font("Calibri", Font.PLAIN, 15));
		usernameField.setHorizontalAlignment(SwingConstants.CENTER);
		usernameField.setColumns(10);
		
		loginPanel = new JPanel();
		loginPanel.setBackground(new Color(0, 204, 204));
		
		loginButton = new JButton("Login");
		loginButton.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				if(usernameField.getText().equals("Abram") && passwordField.getText().equals("test")){
					EventQueue.invokeLater(new Runnable(){
						public void run(){
							try{
								usernameField.setText("");
								passwordField.setText("");
								dispose();
								UIChoose.getUIChooseInstance().setVisible(true);
							}
							catch(Exception e){
								e.printStackTrace();
							}
						}
					});
				}
				else{
					UICredentialError.getCredentialErrorInstance().setVisible(true);
					EventQueue.invokeLater(new Runnable(){
						public void run(){
							try{
								Thread.sleep(1500);
								UICredentialError.getCredentialErrorInstance().dispose();
							}
							catch(Exception e){
								e.printStackTrace();
							}
						}
					});
				}
			}
		});
		loginButton.setBackground(new Color(0, 153, 102));
		loginButton.setFont(new Font("Microsoft Tai Le", Font.BOLD, 14));
		
		getRootPane().setDefaultButton(loginButton);

		cancelButton = new JButton("Clear");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				passwordField.setText("");
				usernameField.setText("");
			}
		});
		cancelButton.setFont(new Font("Microsoft Tai Le", Font.BOLD, 14));
		cancelButton.setBackground(new Color(0, 153, 102));
		GroupLayout gl_loginPanel = new GroupLayout(loginPanel);
		gl_loginPanel.setHorizontalGroup(
			gl_loginPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_loginPanel.createSequentialGroup()
					.addContainerGap(52, Short.MAX_VALUE)
					.addComponent(loginButton, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(cancelButton, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
					.addGap(44))
		);
		gl_loginPanel.setVerticalGroup(
			gl_loginPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_loginPanel.createSequentialGroup()
					.addGap(23)
					.addGroup(gl_loginPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(cancelButton, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
						.addComponent(loginButton, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(25, Short.MAX_VALUE))
		);
		loginPanel.setLayout(gl_loginPanel);

		GroupLayout gl_bodyPanel = new GroupLayout(bodyPanel);
		gl_bodyPanel.setHorizontalGroup(
			gl_bodyPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bodyPanel.createSequentialGroup()
					.addGroup(gl_bodyPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_bodyPanel.createSequentialGroup()
							.addGap(33)
							.addComponent(lblNewLabel)
							.addGap(57)
							.addGroup(gl_bodyPanel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(passwordLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(usernameLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGap(18)
							.addGroup(gl_bodyPanel.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(passwordField)
								.addComponent(usernameField, GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)))
						.addGroup(gl_bodyPanel.createSequentialGroup()
							.addGap(275)
							.addComponent(loginPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		gl_bodyPanel.setVerticalGroup(
			gl_bodyPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bodyPanel.createSequentialGroup()
					.addGroup(gl_bodyPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_bodyPanel.createSequentialGroup()
							.addGap(19)
							.addComponent(lblNewLabel))
						.addGroup(gl_bodyPanel.createSequentialGroup()
							.addGroup(gl_bodyPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_bodyPanel.createSequentialGroup()
									.addGap(34)
									.addComponent(usernameLabel))
								.addGroup(gl_bodyPanel.createSequentialGroup()
									.addGap(53)
									.addComponent(usernameField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)))
							.addGroup(gl_bodyPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_bodyPanel.createSequentialGroup()
									.addGap(18)
									.addComponent(passwordLabel))
								.addGroup(gl_bodyPanel.createSequentialGroup()
									.addGap(31)
									.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))))
					.addPreferredGap(ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
					.addComponent(loginPanel, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		bodyPanel.setLayout(gl_bodyPanel);
		
		JLabel welcomeLabel = new JLabel("WELCOME TO FRIDGE");
		welcomeLabel.setToolTipText("");
		welcomeLabel.setIcon(new ImageIcon(UIMain.class.getResource("/ui/recources/welcome.png")));
		welcomeLabel.setFont(new Font("Calibri", Font.BOLD, 17));
		welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout gl_welcomePanel = new GroupLayout(welcomePanel);
		gl_welcomePanel.setHorizontalGroup(
			gl_welcomePanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_welcomePanel.createSequentialGroup()
					.addComponent(welcomeLabel, GroupLayout.PREFERRED_SIZE, 302, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(300, Short.MAX_VALUE))
		);
		gl_welcomePanel.setVerticalGroup(
			gl_welcomePanel.createParallelGroup(Alignment.LEADING)
				.addComponent(welcomeLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);
		welcomePanel.setLayout(gl_welcomePanel);
		
		creditPanel = new JPanel();
		creditPanel.setBackground(new Color(0, 204, 204));
		
		creditLabel1 = new JLabel("Designed by");
		creditLabel1.setHorizontalAlignment(SwingConstants.CENTER);
		creditLabel1.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		creditLabel2 = new JLabel("Abram Lawendy");
		creditLabel2.setHorizontalAlignment(SwingConstants.CENTER);
		creditLabel2.setFont(new Font("Tahoma", Font.PLAIN, 12));
		GroupLayout gl_creditPanel = new GroupLayout(creditPanel);
		gl_creditPanel.setHorizontalGroup(
			gl_creditPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_creditPanel.createSequentialGroup()
					.addGroup(gl_creditPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(creditLabel1, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
						.addComponent(creditLabel2, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_creditPanel.setVerticalGroup(
			gl_creditPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_creditPanel.createSequentialGroup()
					.addComponent(creditLabel1, GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(creditLabel2)
					.addGap(12))
		);
		creditPanel.setLayout(gl_creditPanel);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(5)
					.addComponent(poweredPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 399, Short.MAX_VALUE)
					.addComponent(creditPanel, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(132)
							.addComponent(welcomePanel, GroupLayout.PREFERRED_SIZE, 302, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(10)
							.addComponent(bodyPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(11)
					.addComponent(welcomePanel, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
					.addGap(11)
					.addComponent(bodyPanel, GroupLayout.PREFERRED_SIZE, 266, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(poweredPanel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(creditPanel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)))
		);
		contentPane.setLayout(gl_contentPane);
	}

	public static UIMain getUIMainInstance(){
		if(uiMainInstance == null)
			uiMainInstance = new UIMain();
		return uiMainInstance;
	}
}
